﻿using IM.Easy.Core.ServiceLifetime;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Services
{
    public abstract class AppServices(ISqlSugarClient db) : IAppServices
    {
        // 定义一个只读属性来存储 ISqlSugarClient 实例
        protected ISqlSugarClient Db { get; } = db;

    }

    public abstract class AppServices<T> : SimpleClient<T>, IAppServices where T : class, new()
    {
        // 定义一个只读属性来存储 ISqlSugarClient 实例
        protected ISqlSugarClient Db { get; }
        public AppServices(ISqlSugarClient db)
        {
            Db = db;
            base.Context = db;
        }

        public virtual bool Update(Expression<Func<T, bool>> columns, Expression<Func<T, bool>> whereExpression)
        {
            return Context.Updateable<T>().SetColumns(columns).Where(whereExpression)
                .ExecuteCommand() > 0;
        }

        /// <summary>
        /// 保存或更新实体
        /// </summary>
        /// <param name="data"></param>
        /// <returns>返回保存或更新后的实体</returns>
        public virtual T InsertOrUpdateReturnEntity(T data)
        {
            return Context.Storageable(data).ExecuteReturnEntity();
        }


    }


}
